import { ref, Ref } from 'vue'

/**
 * 生成 ref 响应式状态
 *
 * @template T 状态的类型
 * @param defaultVal 状态的默认值
 * @returns {[Ref<T>, () => T, (newVal: T) => void]} 返回一个数组，数组中包含三个元素：
 * - 1. ref 响应式状态
 * - 2. 获取状态值的方法
 * - 3. 设置状态值的方法
 */
export const genStatus = <T>(defaultVal: T): [Ref<T>, () => T, (newVal: T) => void] => {
  const status = ref<T>(defaultVal) as Ref<T>
  const getter = () => status.value
  const setter = (newVal: T) => (status.value = newVal)
  return [status, getter, setter]
}

export type GenStatusType = typeof genStatus
